package com.zhoug.mpchart.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.zhoug.mpchart.R;

import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;

/**
 * 图表的Marker 圆形或矩形颜色label编辑
 *
 * @Author 35574
 * @Date 2020/9/14
 * @Description
 */
public class MPIconView extends View {
    private static final String TAG = ">>>MarkerLabelIcon";
    private static final boolean DEBUG = false;
    public static final int NULL=0;
    public static final int CIRCLE = 1;//圆
    public static final int RING = 2;//环
    public static final int RECTANGLE = 3;//矩形
    public static final int RECTANGLE_ARC = 4;//半圆矩形
    public static final int LINE = 5;//线
    public static final int LINE_CIRCLE = 6;//线中间圆孔
    private Paint mPaint;
    private int color;
    private int circleColor;//线中间圆形的颜色
    private int circleHoleColor;//线中间孔洞的颜色
    /**
     * {@link #CIRCLE,#RING,#RECTANGLE,#LINE,#RECTANGLE_ARC}
     */
    private int shape = CIRCLE;
    private float mStrokeWidth;
    private Path mPath = new Path();
    private RectF mRectF = new RectF();

    public MPIconView(Context context) {
        this(context, null);

    }

    public MPIconView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MPIconView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MPIconView, defStyleAttr, 0);
        color = a.getColor(R.styleable.MPIconView_mp_color, 0);
        circleColor = a.getColor(R.styleable.MPIconView_mp_circleColor, color);
        circleHoleColor = a.getColor(R.styleable.MPIconView_mp_circleHoleColor, Color.WHITE);
        shape = a.getInt(R.styleable.MPIconView_mp_shape, RECTANGLE);
        mStrokeWidth = a.getDimension(R.styleable.MPIconView_mp_strokeWidth, 0);

        a.recycle();
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
        if (DEBUG) {
            Log.d(TAG, "init:ringSize=" + mStrokeWidth);
        }

        setColor(color);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        if (shape == CIRCLE) {
            //圆
            int width = getWidth();
            int height = getHeight();
            float r = width > height ? height >> 1 : width >> 1;
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setStrokeWidth(0);
            mPaint.setColor(color);
            canvas.drawCircle(width >> 1, height >> 1, r, mPaint);
        } else if (shape == RING) {
            //环
            int width = getWidth();
            int height = getHeight();
            int min = Math.min(width, height);
            float r = min/2.0f;
            if (mStrokeWidth == 0) {
                mStrokeWidth = min / 6.0f;
            }
            if (mStrokeWidth % 2 != 0) {
                mStrokeWidth += 1;
            }
            float halfStrokeWidth = mStrokeWidth / 2;
            r = r - halfStrokeWidth;
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeWidth(mStrokeWidth);
            mPaint.setColor(color);
            canvas.drawCircle(width >> 1, height >> 1, r, mPaint);
        } else if (shape == LINE) {
            //线
            int height = getHeight();
            if (mStrokeWidth == 0) {
                mStrokeWidth = height / 6.0f;
            }
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setStrokeWidth(mStrokeWidth);
            mPaint.setColor(color);
            float centerY=height/2.0f;
            canvas.drawLine(0,centerY,getWidth(),centerY, mPaint);

        } else if (shape == LINE_CIRCLE) {
            //LINE_CIRCLE
            int width = getWidth();
            int height = getHeight();
            float centerX = width /2.0f;
            float centerY = height /2.0f;
            int min = Math.min(width, height);
            float r = min / 3.0f;
            if (mStrokeWidth == 0) {
                mStrokeWidth = min / 6.0f;
            }
            if (mStrokeWidth % 2 != 0) {
                mStrokeWidth += 1;
            }
            float halfStrokeWidth = mStrokeWidth / 2;
            r = r - halfStrokeWidth;


            mPaint.setColor(color);
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setStrokeWidth(mStrokeWidth);
            canvas.drawLine(0, centerY, centerX - r , centerY, mPaint);
            canvas.drawLine(centerX + r , centerY, width, centerY, mPaint);

            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setColor(circleColor);
            canvas.drawCircle(centerX, centerY, r, mPaint);

            mPaint.setColor(circleHoleColor);
            mPaint.setStyle(Paint.Style.FILL);
            canvas.drawCircle(centerX, centerY, r - halfStrokeWidth, mPaint);



        } else if (shape == RECTANGLE_ARC) {
            //半圆矩形
            float width = getWidth();
            float height = getHeight();
            float offset = width;
            if (height < offset) {
                offset = height;
            }
            mRectF.left = 0;
            mRectF.top = 0;
            mRectF.right = width;
            mRectF.bottom = mRectF.top + offset;
            mPath.reset();
            mPath.moveTo(mRectF.right, mRectF.bottom);
            mPath.lineTo(mRectF.right, height);
            mPath.lineTo(mRectF.left, height);
            mPath.lineTo(mRectF.left, mRectF.bottom);
            mPath.arcTo(mRectF, 180, 180, false);
            mPaint.setColor(color);
            canvas.drawPath(mPath, mPaint);
        } else {
            //矩形
            if (color != 0) {
                canvas.drawColor(color);
            }
        }
        super.onDraw(canvas);
    }


    public void setColor(@ColorInt int color) {
        this.color = color;
    }

    public void setCircleColor(int circleColor) {
        this.circleColor = circleColor;
    }

    public void setCircleHoleColor(int circleHoleColor) {
        this.circleHoleColor = circleHoleColor;
    }

    /**
     * {@link #CIRCLE,#RING,#RECTANGLE,#LINE,#RECTANGLE_ARC}
     *
     * @param shape
     */
    public void setShape(int shape) {
        this.shape = shape;
    }

    public void setStrokeWidth(int strokeWidth) {
        this.mStrokeWidth = strokeWidth;
    }

}
